//
//  GQTabBarController.m
//  LotteryProject
//
//  Created by Ezra on 16/5/18.
//  Copyright © 2016年 Ezra. All rights reserved.
//

#import "GQTabBarController.h"
#import "GQHallTableViewController.h"
#import "GQArenaViewController.h"
#import "GQMyLotteryViewController.h"
#import "GQDiscoverTableViewController.h"
#import "GQHistoryTableViewController.h"

#import "GQArenaNavViewController.h"
#import "GQNavigationViewController.h"
#import "GQTabBar.h"

@interface GQTabBarController ()<GQTabBarDelegate>

@property (nonatomic, strong)NSMutableArray *items;

@end

@implementation GQTabBarController

-(NSMutableArray *)items
{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self setupAllChildViewController];

    [self setupTabBar];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //移除之后，还需要在图形化界面即storyboard中设置hide bottom bar on push选项
    for (UIView *view in self.tabBar.subviews) {
        if (![view isKindOfClass:[GQTabBar class]]) {
            [view removeFromSuperview];
        }
    }
}
- (void)setupTabBar
{
    GQTabBar *tabBar = [[GQTabBar alloc]init];
    
    tabBar.frame = self.tabBar.bounds;

    //把自定义的tabbar加入到原生的tabbar中
    [self.tabBar addSubview:tabBar];
    
    tabBar.items = self.items;
    
    tabBar.delegate = self;
    
    
}

//设置栈顶控制器 顺序决定tabbar的顺序
- (void)setupAllChildViewController
{
    //购彩大厅
    GQHallTableViewController *hall = [[GQHallTableViewController alloc]init];
    [self setupOneChildViewController:hall image:[UIImage imageNamed:@"TabBar_Hall_new"] selImage:[UIImage imageNamed:@"TabBar_Hall_selected_new"] title:@"购彩大厅"];
    
    //竞技场
    GQArenaViewController *arena = [[GQArenaViewController alloc]init];
    [self setupOneChildViewController:arena image:[UIImage imageNamed:@"TabBar_Arena_new"] selImage:[UIImage imageNamed:@"TabBar_Arena_selected_new"] title:@"竞技场"];
    
    //发现
    GQDiscoverTableViewController *discover = [[GQDiscoverTableViewController alloc]init];
    [self setupOneChildViewController:discover image:[UIImage imageNamed:@"TabBar_Discovery_new"] selImage:[UIImage imageNamed:@"TabBar_Discovery_selected_new"] title:@"发现"];
    
    //开奖信息
    GQHistoryTableViewController *history = [[GQHistoryTableViewController alloc]init];
    [self setupOneChildViewController:history image:[UIImage imageNamed:@"TabBar_History_new"] selImage:[UIImage imageNamed:@"TabBar_History_selected_new"] title:@"开奖信息"];
    
    //我的彩票
    GQMyLotteryViewController *myLottery = [[GQMyLotteryViewController alloc]init];
    [self setupOneChildViewController:myLottery image:[UIImage imageNamed:@"TabBar_MyLottery_new"] selImage:[UIImage imageNamed:@"TabBar_MyLottery_selected_new"] title:@"我的彩票"];
    
}
//设置单个导航控制器，
- (void)setupOneChildViewController:(UIViewController *)vc image:(UIImage *)image selImage:(UIImage *)selImage title:(NSString *)title
{
    UINavigationController *nav = [[GQNavigationViewController alloc]initWithRootViewController:vc];
    
    if ([vc isKindOfClass:[GQArenaViewController class]]) {
        nav = [[GQArenaNavViewController alloc]initWithRootViewController:vc];
    }
    
    //设置title 用navitem
    vc.navigationItem.title = title;
    
    [self addChildViewController:nav];
    
    //设置tabbaritem
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selImage;
    
    [self.items addObject:vc.tabBarItem];
    
    
}

#pragma mark delegate
- (void)tabBar:(GQTabBar *)tabBar index:(NSInteger)index
{
    self.selectedIndex = index;
}

@end
